# Clear workspace
rm(list = ls())

library(sp)
library(RColorBrewer)
library(classInt)
library(spdep)
library(LambertW)
library(cluster)
library(geodata)

pal <- brewer.pal(7, "OrRd") # we select 7 colors from the palette

setwd("C:/Thailand_Welfare_Weather")         # <- Please change your working directory here

projcrs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

tha_1 <-st_as_sf(terra::vect("./Data/ShapeFiles/", "gadm41_THA_1"), crs=projcrs)

st_crs(tha_1) =  projcrs

library("haven")
tha_maps_data<- read_dta("./Processed_Data/data_maps.dta")
tha_maps_data_poor_years <- read_dta("./Processed_Data/data_maps_poor_overall.dta")

THA_1_sf_merged <- merge(tha_1, tha_maps_data, by="GID_1",all.x=TRUE, all.y=FALSE )

THA_1_sf_merged_pov_years <- merge(tha_1, tha_maps_data_poor_years, by="GID_1",all.x=TRUE, all.y=FALSE )

#######

library(dplyr)

THA_1_sf_merged_pov_years <- THA_1_sf_merged_pov_years %>%
  filter(year >= 2008 & year <= 2021)


library(tmap)
library(tmaptools)

library(tmap)
library(cols4all)

tmap_mode("plot")

# Rename the variable to control the legend title
THA_1_sf_merged_pov_years$`Poverty Rate` <- THA_1_sf_merged_pov_years$poor_17

tha_poor_fraction <- 
  tm_shape(THA_1_sf_merged_pov_years) +
  tm_polygons(
    fill = "Poverty Rate",
    col = NA,
    fill.scale = tm_scale_continuous(
      values = c4a("brewer.reds", n = 5)
    )
  ) +
  tm_facets(
    by = "year",
    nrow = 2,
    drop.units = TRUE
  ) +
  tm_layout(
    legend.outside = FALSE,
    bg.color = "white",
    scale = 1.0,
    legend.title.size = 1.0,
    inner.margins = c(0.01, 0.01, 0.01, 0.01)
  ) +
  tm_title(
    text = "",
    size = 2
  )

tmap_save(
  tha_poor_fraction,
  filename = "./Graphs/thailand_poor_years.png",
  dpi = 300,
  width = 8,
  height = 4,
  units = "in"
)
library(tmap)
library(tmap)
library(magick)
library(cols4all)

tmap_mode("plot")
tmap_options(component.autoscale = FALSE)

make_map <- function(shape, var, palette_name, title_text) {
  palette_vals <- c4a(palette_name, n = 5)
  
  tm_shape(shape, bbox.show = FALSE) +
    tm_polygons(
      fill = var,
      fill.scale = tm_scale_continuous(values = palette_vals),
      fill.legend = tm_legend(title = "Proportion")
    ) +
    tm_title(title_text, size = 1.0) +
    tm_layout(
      legend.show = TRUE,
      legend.outside = FALSE,
      legend.frame = FALSE,  # ← removes the box
      inner.margins = c(0.05, 0.02, 0.10, 0.02),
      frame = FALSE
    )
}


# Generate maps with unified legend title
map1 <- make_map(THA_1_sf_merged, "rice_fraction", "brewer.greens", "Rice")
map2 <- make_map(THA_1_sf_merged, "rice_fraction_irrigated", "brewer.blues", "Prop. Rice that is Irrigated")
map3 <- make_map(THA_1_sf_merged, "rubber_poil_fraction", "brewer.reds", "Rubber/Palm Oil")
map4 <- make_map(THA_1_sf_merged, "fruit_fraction", "brewer.greys", "Fruit")

# Save each map
tmap_save(map1, filename = "./Graphs/map1_rice.png", width = 3, height = 4, dpi = 300)
tmap_save(map2, filename = "./Graphs/map2_irrigated.png", width = 3, height = 4, dpi = 300)
tmap_save(map3, filename = "./Graphs/map3_rubber.png", width = 3, height = 4, dpi = 300)
tmap_save(map4, filename = "./Graphs/map4_fruit.png", width = 3, height = 4, dpi = 300)

# Combine into one row
combined <- image_append(c(
  image_read("./Graphs/map1_rice.png"),
  image_read("./Graphs/map2_irrigated.png"),
  image_read("./Graphs/map3_rubber.png"),
  image_read("./Graphs/map4_fruit.png")
))

# Save final composite image
image_write(combined, path = "./Graphs/thailand_crop_maps_combined.png", format = "png")
